// pages/detail/index.js
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    hide:!1,
    show:!0,
    url:'/pages/goods/index',
    userid: wx.BaaS.storage.get('userId'),
    collection_image: ["../../images/sc.svg", "../../images/sc_hover.svg"],
    collection_image_index: 0,
    isupdata: 0,
    tableID: 50450
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var t = this;
    var id = options.id;
    t.getTreeMessage(id,function(res){
      t.setData({
        show:!1,
        message:res.data,
        treeid: res.data.id,
        url : t.data.url + '?id=' + id
      })
    });
    this.getCollection(id);
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },
  getTreeMessage : function(id,fn){//获取树的信息
    setTimeout(function(){
      var url = app.getUrl() + '/planttingTree/tree';
      var data = {
        id: id,
        type: 'getTree',
        memberid: wx.BaaS.storage.get('userId')
      }
      app.core(url,'GET',data,function(res){
        fn(res);
      })
    },1000);
  },
  collection: function(){//收藏树
    let index = this.data.collection_image_index == 0 ? 1:0;
    this.setData({
      collection_image_index: index,
    });
    
    let sqltype = this.data.isupdata ? 'update':'save';
    this.updataAndSave(sqltype);
  },
  updataAndSave: function (sqltype){//添加收藏记录or跟新收藏状态
    var url = app.getUrl() + '/planttingTree/tree';
    var data = {
      type: 'memberCollection',
      userid: wx.BaaS.storage.get('userId'),
      treeid: this.data.message.id,
      sqltype: sqltype,
      iscollection: this.data.collection_image_index
    };
    app.core(url,'GET',data,function(res){console.log(res)})
  },
  getCollection: function(treeid){//获取收藏信息
    var url = app.getUrl() + '/planttingTree/tree';
    var t =this;
    var data = {
      type: 'getCollection',
      userid: wx.BaaS.storage.get('userId'),
      treeid: treeid
    };
    app.core(url, 'GET', data, function (res) {
      if(!res.data){//没有获取收藏记录的时候的处理
        return;//跳出函数 不执行下面的代码
      }
      t.setData({
        isupdata: 1,
        recordID: res.data.id,
        collection_image_index: res.data.iscollection
      })
    })
  } 
})